<!DOCTYPE html>
<html>
  <title>计算属性复杂使用</title>
  <script src="../../js/vue.js"></script>
  <head>
    <meta charset="utf-8" />
  </head>
  <body>
    <div id="div1">
      <h2>{{totalPrice}}</h2>
    </div>
  </body>
  <script>
    const app = new Vue({
      el: "#div1",
      data: {
        books: [
          { id: 101, name: "Java编程思想", price: 100.0 },
          { id: 102, name: "算法", price: 79.1 },
          { id: 103, name: "设计模式", price: 131.0 },
          { id: 104, name: "深入Linux", price: 80.0 }
        ]
      },
      computed: {
        totalPrice: function() {
          let res = 0.0;
          for (let i = 0; i < this.books.length; ++i) {
            res += this.books[i].price;
          }
          return res;
        }
      }
    });
  </script>
</html>
